<?php 
namespace App\Console\Commands;

include base_path() . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Log;
use DB;
use App\User;
use App\Agent;
use App\SuperAgent;
use App\RebateLog;
use App\Order;

class SendReward extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'send:reward';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders = Order::where('id', '>', 1944)->where(['pay_status' => 1])->get();
        foreach ($orders as $order) {
            $user = User::where(['userid' => $order->user_id])->first();
            if ($user) {
                $this->userDivid($user, $order);
            }
        }
    }

        /**
     * 代理分成返利
     */
    public function userDivid($user, $order)
    {
        $parent_daili = Agent::where(['id' => $user->daili_id])->first();
        if ($parent_daili) {
            switch ($parent_daili->level) {
                case 1://二级代理
                    if ($parent_daili->superagent_id != 0) {//有一级代理
                        $agent_one = SuperAgent::where(['id' => $parent_daili->superagent_id])->first();
                        if ($agent_one) {
                            $this->dividOne($agent_one, $parent_daili, $order);
                        }
                    } else {//没有一级代理
                        $this->dividTwo($parent_daili, $order);
                    }
                    break;
                case 2://三级代理
                    $agent_two = Agent::where(['id' => $parent_daili->pid])->first();
                    if ($agent_two->superagent_id != 0) {
                        $agent_one = SuperAgent::where(['id' => $agent_two->superagent_id])->first();
                        if ($agent_one) {
                            $this->dividThree($agent_one, $agent_two, $parent_daili, $order);
                        }
                    } else {
                        $this->dividFour($agent_two, $parent_daili, $order);
                    }
                    break;
                case 3://四级代理
                    $agent_three = Agent::where(['id' => $parent_daili->pid])->first();
                    if ($agent_three) {
                        $agent_two = Agent::where(['id' => $agent_three->pid])->first();
                        if ($agent_two->superagent_id != 0) {
                            $agent_one = SuperAgent::where(['id' => $agent_two->superagent_id])->first();
                            if ($agent_one) {
                                $this->dividFive($agent_one, $agent_two, $agent_three, $parent_daili, $order);
                            }
                        } else {
                            $this->dividSix($agent_two, $agent_three, $parent_daili, $order);
                        }
                    }
                    break;
            }
        }
        return;
    }

    public function dividOne($agent_one, $agent_two, $order) 
    {
        $fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100);//二级代理分钱
        $fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two;//一级代理分钱
        $agent_one->money += $fee_one;
        $agent_one->save();
        $agent_two->money += $fee_two;
        $agent_two->save();
        $log = new RebateLog;
        $log->order_sn = $order->order_sn;
        $log->type = 1;
        $log->daili_id = $agent_one->id;
        $log->second_id = $agent_two->id;
        $log->user_id = $order->user_id;
        $log->fee = $fee_one;
        $log->save();
        $log_two = new RebateLog;
        $log_two->order_sn = $order->order_sn;
        $log_two->type = 2;
        $log_two->daili_id = $agent_one->id;
        $log_two->second_id = $agent_two->id;
        $log_two->user_id = $order->user_id;
        $log_two->fee = $fee_two;
        $log_two->save();
        return;
    }

    public function dividTwo($agent_two, $order)
    {
        $fee_two = $order->order_amount * ($agent_two->divide / 100);
        $agent_two->money += $fee_two;
        $agent_two->save();
        $log = new RebateLog;
        $log->order_sn = $order->order_sn;
        $log->type = 2;
        $log->second_id = $agent_two->id;
        $log->user_id = $order->user_id;
        $log->fee = $fee_two;
        $log->save();
        return;
    }

    public function dividThree($agent_one, $agent_two, $agent_three, $order)
    {
        $fee_three = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100);
        $fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) - $fee_three;
        $fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two - $fee_three;
        $agent_one->money += $fee_one;
        $agent_one->save();
        $agent_two->money += $fee_two;
        $agent_two->save();
        $agent_three->money += $fee_three;
        $agent_three->save();
        $log_one = new RebateLog;
        $log_one->order_sn = $order->order_sn;
        $log_one->type = 1;
        $log_one->daili_id = $agent_one->id;
        $log_one->second_id = $agent_two->id;
        $log_one->third_id = $agent_three->id;
        $log_one->user_id = $order->user_id;
        $log_one->fee = $fee_one;
        $log_one->save();
        $log_two = new RebateLog;
        $log_two->order_sn = $order->order_sn;
        $log_two->type = 2;
        $log_two->daili_id = $agent_one->id;
        $log_two->second_id = $agent_two->id;
        $log_two->third_id = $agent_three->id;
        $log_two->user_id = $order->user_id;
        $log_two->fee = $fee_two;
        $log_two->save();
        $log_three = new RebateLog;
        $log_three->order_sn = $order->order_sn;
        $log_three->type = 3;
        $log_three->daili_id = $agent_one->id;
        $log_three->second_id = $agent_two->id;
        $log_three->third_id = $agent_three->id;
        $log_three->user_id = $order->user_id;
        $log_three->fee = $fee_three;
        $log_three->save();
        return;
    }

    public function dividFour($agent_two, $agent_three, $order)
    {
        $fee_three = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100);  
        $fee_two = $order->order_amount * ($agent_two->divide / 100) - $fee_three;
        $agent_two->money += $fee_two;
        $agent_two->save();
        $agent_three->money += $fee_three;
        $agent_three->save();
        $log_two = new RebateLog;
        $log_two->order_sn = $order->order_sn;
        $log_two->type = 2;
        $log_two->second_id = $agent_two->id;
        $log_two->third_id = $agent_three->id;
        $log_two->user_id = $order->user_id;
        $log_two->fee = $fee_two;
        $log_two->save();
        $log_three = new RebateLog;
        $log_three->order_sn = $order->order_sn;
        $log_three->type = 3;
        $log_three->second_id = $agent_two->id;
        $log_three->third_id = $agent_three->id;
        $log_three->user_id = $order->user_id;
        $log_three->fee = $fee_three;
        $log_three->save();
        return;
    }

    public function dividFive($agent_one, $agent_two, $agent_three, $agent_four, $order)
    {
        $fee_four = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100) * ($agent_four->divide / 100);
        $fee_three = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) * ($agent_three->divide / 100) - $fee_four;
        $fee_two = $order->order_amount * ($agent_one->divide / 100) * ($agent_two->divide / 100) - $fee_three - $fee_four;
        $fee_one = $order->order_amount * ($agent_one->divide / 100) - $fee_two - $fee_three - $fee_four;
        $agent_one->money += $fee_one;
        $agent_one->save();
        $agent_two->money += $fee_two;
        $agent_two->save();
        $agent_three->money += $fee_three;
        $agent_three->save();
        $agent_four->money += $fee_four;
        $agent_four->save();
        $log_one = new RebateLog;
        $log_one->order_sn = $order->order_sn;
        $log_one->type = 1;
        $log_one->daili_id = $agent_one->id;
        $log_one->second_id = $agent_two->id;
        $log_one->third_id = $agent_three->id;
        $log_one->fourth_id = $agent_four->id;
        $log_one->user_id = $order->user_id;
        $log_one->fee = $fee_one;
        $log_one->save();
        $log_two = new RebateLog;
        $log_two->order_sn = $order->order_sn;
        $log_two->type = 2;
        $log_two->daili_id = $agent_one->id;
        $log_two->second_id = $agent_two->id;
        $log_two->third_id = $agent_three->id;
        $log_two->fourth_id = $agent_four->id;
        $log_two->user_id = $order->user_id;
        $log_two->fee = $fee_two;
        $log_two->save();
        $log_three = new RebateLog;
        $log_three->order_sn = $order->order_sn;
        $log_three->type = 3;
        $log_three->daili_id = $agent_one->id;
        $log_three->second_id = $agent_two->id;
        $log_three->third_id = $agent_three->id;
        $log_three->fourth_id = $agent_four->id;
        $log_three->user_id = $order->user_id;
        $log_three->fee = $fee_three;
        $log_three->save();
        $log_four = new RebateLog;
        $log_four->order_sn = $order->order_sn;
        $log_four->type = 4;
        $log_four->daili_id = $agent_one->id;
        $log_four->second_id = $agent_two->id;
        $log_four->third_id = $agent_three->id;
        $log_four->fourth_id = $agent_four->id;
        $log_four->user_id = $order->user_id;
        $log_four->fee = $fee_four;
        $log_four->save();
        return;
    }

    public function dividSix($agent_two, $agent_three, $agent_four, $order)
    {
        $fee_four = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100) * ($agent_four->divide / 100);
        $fee_three = $order->order_amount * ($agent_two->divide / 100) * ($agent_three->divide / 100) - $fee_four;
        $fee_two = $order->order_amount * ($agent_two->divide / 100) - $fee_three - $fee_four;
        $agent_two->money += $fee_two;
        $agent_two->save();
        $agent_three->money += $fee_three;
        $agent_three->save();
        $agent_four->money += $fee_four;
        $agent_four->save();
        $log_four = new RebateLog;
        $log_four->order_sn = $order->order_sn;
        $log_four->type = 4;
        $log_four->second_id = $agent_two->id;
        $log_four->third_id = $agent_three->id;
        $log_four->fourth_id = $agent_four->id;
        $log_four->user_id = $order->user_id;
        $log_four->fee = $fee_four;
        $log_four->save();
        $log_three = new RebateLog;
        $log_three->order_sn = $order->order_sn;
        $log_three->type = 3;
        $log_three->second_id = $agent_two->id;
        $log_three->third_id = $agent_three->id;
        $log_three->fourth_id = $agent_four->id;
        $log_three->user_id = $order->user_id;
        $log_three->fee = $fee_three;
        $log_three->save();
        $log_two = new RebateLog;
        $log_two->order_sn = $order->order_sn;
        $log_two->type = 2;
        $log_two->second_id = $agent_two->id;
        $log_two->third_id = $agent_three->id;
        $log_two->fourth_id = $agent_four->id;
        $log_two->user_id = $order->user_id;
        $log_two->fee = $fee_two;
        $log_two->save();
        return;
    }

}
